//
//  PosOrderController.m
//  PosApp
//
//  Created by 刘萍 on 2018/9/13.
//  Copyright © 2018年 李英新. All rights reserved.
//

#import "PosOrderController.h"
#import "SPPageMenu.h"
#import "ChildController01.h"
#import "ChildController02.h"
#import "ChildController03.h"
#import "ChildController04.h"

#define pageMenuH 34
#define ScrollVH  KScreenH-TopH-BotL-pageMenuH
@interface PosOrderController ()<SPPageMenuDelegate, UIScrollViewDelegate>
@property (nonatomic, strong) NSArray *dataArr;
@property (nonatomic, weak) SPPageMenu *pageMenu;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *myChildViewControllers;

@end

@implementation PosOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"POS订单";
    self.dataArr = @[@"我的POS",@"划拨记录",@"待付款",@"待收货"];
    
    // trackerStyle:跟踪器的样式
    SPPageMenu *pageMenu = [SPPageMenu pageMenuWithFrame:CGRectMake(0, 0, KScreenW, pageMenuH) trackerStyle:SPPageMenuTrackerStyleLine];
    pageMenu.backgroundColor = [UIColor whiteColor];
    pageMenu.dividingLine.backgroundColor = [UIColor whiteColor];
    // 传递数组，默认选中第1个
    [pageMenu setItems:self.dataArr selectedItemIndex:0];
    // 不可滑动的自适应内容排列
    pageMenu.permutationWay = SPPageMenuPermutationWayNotScrollAdaptContent;
    pageMenu.selectedItemTitleColor = [UIColor colorWithRed:251/255.0 green:40/255.0 blue:68/255.0 alpha:1];
    pageMenu.unSelectedItemTitleColor = [UIColor colorWithRed:88/255.0 green:88/255.0 blue:88/255.0 alpha:1];
    pageMenu.tracker.backgroundColor = [UIColor colorWithRed:251/255.0 green:40/255.0 blue:68/255.0 alpha:1];
    // 设置代理
    pageMenu.delegate = self;
    [self.view addSubview:pageMenu];
    _pageMenu = pageMenu;
    
    ChildController01 * GW01 = [[ChildController01 alloc] init];
    [self addChildViewController:GW01];
    [self.myChildViewControllers addObject:GW01];
    
    ChildController02 * GW02 = [[ChildController02 alloc] init];
    [self addChildViewController:GW02];
    [self.myChildViewControllers addObject:GW02];
    
    ChildController03 * GW03 = [[ChildController03 alloc] init];
    [self addChildViewController:GW03];
    [self.myChildViewControllers addObject:GW03];
    
    ChildController04 * GW04 = [[ChildController04 alloc] init];
    [self addChildViewController:GW04];
    [self.myChildViewControllers addObject:GW04];
    
    UILabel * line = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(pageMenu.frame), KScreenW, 1)];
    line.backgroundColor = KRGBColor(243, 243, 243);
    [self.view addSubview:line];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, pageMenuH+1, KScreenW, ScrollVH)];
    //    scrollView.backgroundColor = [UIColor yellowColor];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    _scrollView = scrollView;
    
    //这一行赋值，可实现pageMenu的跟踪器时刻跟随scrollView滑动的效果
    self.pageMenu.bridgeScrollView = self.scrollView;
    //pageMenu.selectedItemIndex就是选中的item下标
    if (self.pageMenu.selectedItemIndex < self.myChildViewControllers.count) {
        self.childViewControllers[self.pageMenu.selectedItemIndex].view.frame = CGRectMake(KScreenW*self.pageMenu.selectedItemIndex, 0, KScreenW, ScrollVH);
        [scrollView addSubview:self.childViewControllers[self.pageMenu.selectedItemIndex].view];
        
        scrollView.contentOffset = CGPointMake(KScreenW*self.pageMenu.selectedItemIndex, 0);
        scrollView.contentSize = CGSizeMake(self.dataArr.count*KScreenW, 0);
    }
}

#pragma mark - SPPageMenu的代理方法

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedAtIndex:(NSInteger)index {
    NSLog(@"%zd",index);
}

- (void)pageMenu:(SPPageMenu *)pageMenu itemSelectedFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    
    NSLog(@"%zd------->%zd",fromIndex,toIndex);
    // 如果fromIndex与toIndex之差大于等于2,说明跨界面移动了,此时不动画.
    if (labs(toIndex - fromIndex) >= 2) {
        [self.scrollView setContentOffset:CGPointMake(KScreenW * toIndex, 0) animated:NO];
    } else {
        [self.scrollView setContentOffset:CGPointMake(KScreenW * toIndex, 0) animated:YES];
    }
    if (self.myChildViewControllers.count <= toIndex) {return;}
    
    UIViewController *targetViewController = self.myChildViewControllers[toIndex];
    // 如果已经加载过，就不再加载
    if ([targetViewController isViewLoaded]) return;
    
    targetViewController.view.frame = CGRectMake(KScreenW * toIndex, 0, KScreenW, ScrollVH);
    [_scrollView addSubview:targetViewController.view];
    
}

#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 这一步是实现跟踪器时刻跟随scrollView滑动的效果,如果对self.pageMenu.scrollView赋了值，这一步可省
    // [self.pageMenu moveTrackerFollowScrollView:scrollView];
}

#pragma mark - getter

- (NSMutableArray *)myChildViewControllers {
    
    if (!_myChildViewControllers) {
        _myChildViewControllers = [NSMutableArray array];
        
    }
    return _myChildViewControllers;
}

- (void)dealloc {
    NSLog(@"父控制器被销毁了");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
